//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    let code = "";
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if(res.code){
          code = res.code;
        }else{
          console.log("error msg: " + res.errMsg);
        }
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              let usr = {};
              // 可以将 res 发送给后台解码出 unionId
              wx.request({
                url: 'https://www.limecar.cn/WxApi/GetUserInfo',
                data: {
                  encryptedData: res.encryptedData,
                  iv: res.iv,
                  code: code
                },
                success: function(result){
                  usr = result.data;
                  getApp().globalData.userInfo = usr;
                  console.log(getApp().globalData.userInfo);
                }
              })
              this.globalData.userInfo = usr;
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    appId: "wx08e467df92f2fc87",
    mch_Id: "1241369502",
    body: "青柠养车-微信支付",
    limeKey: "qingningyangche08qingningyangche",
    // limeKey: "19fb44879075c64844ce9049e3c39b8b",
    userInfo: null,
    storeName: "",
    appointment: {},
    checkedSrv: []
  }
})